include_once"str_functions.php";
function isMobile()
{
$info= $_SERVER['HTTP_USER_AGENT'];
$ismobile=false;
if(stristr($info,'ipad') || stristr($info, 'android') || stristr($info, 'iphone') || stristr($info, 'ipod') || stristr($info, 'iemobile')){
$ismobile=true;
}
return $ismobile;
}
function BlockSQLInjection($str)
{
return str_replace(array("'",'"',"'",'"'), array("'",""","'","""), $str);
}
function getimageHeight($size)
{
//$width = $size[0];
$height = $size[1];
return $height;
}
function getimageWidth($size)
{
$width = $size[0];
return $width;
}
function getTblPropArray()
{
	$arry=array('one_', 'two_', 'three_', 'four_', 'five_', 'six_', 'seven_', 'eight_', 'nine_', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen', 'twenty', 'twentyone', 'twentytwo', 'twentythree', 'twentyfour', 'twentyfive', 'twentysix', 'twentyseven', 'twentyeight', 'twentynine', 'thirty', 'thirtyone', 'thirtytwo', 'thirtythree', 'thirtyfour', 'thirtyfive', 'thirtysix', 'thirtyseven', 'thirtyeight', 'thirtynine', 'forty', 'fortyone', 'fortytwo', 'fortythree', 'fortyfour', 'fourtyfive', 'fourtysix', 'fourtyseven', 'fortyeight');
	return $arry;
}
function parseString($stringtoparse, $startdel, $enddel, $mode)
{
if($mode==1)
{
$pos1=strpos($stringtoparse, $startdel);
$stringleft1=substr($stringtoparse, $pos1);
$pos2=strpos($stringleft1, $enddel);
$enddellen=strlen($enddel);
$stringtoreturn=substr($stringleft1, 0, $pos2+$enddellen);
return $stringtoreturn;
}
else if($mode==-1)
{
$pos1=strpos($stringtoparse, $startdel);
$stringleft1=substr($stringtoparse, $pos1+strlen($startdel));
$pos2=strpos($stringleft1, $enddel);
$enddellen=strlen($enddel);
$stringtoreturn=substr($stringleft1, 0, $pos2);
return $stringtoreturn;
}
}
function parseSelectListOptionsData($selectlistcode)
{
$optionsarray=array();
$optionscount=substr_count($selectlistcode, "";
}
function MatchAndMakeLinkTextEntries($stringtosearch, $stringtofind, $url, $whattoreturn)
{
$stringtosearch2=$stringtosearch;
//first check if there is a ".$strtoreplace."";
$linkcodetoignore=$linkcode;
//$stringtosearch2=substr(strstr($stringtosearch2, $linkcodetoignore), $linkcodelength);
//$result=substr()
if($whattoreturn=="1")
{//1==return remaining text not coverted to link
$result=str_replace($strtoreplace, $linkcode, $stringtosearch2);
$resultlength=strlen($result);
$strarry=explode($linkcode, $result);
if($strarry[0]=="")
{
$result=end($strarry);
if($result!="")
{
return $result;
}
}
}
elseif($whattoreturn=="2")
{
//$result=str_replace($strtoreplace, $linkcode, $stringtosearch2);
//$resultlength=strlen($result);
//$strarry=explode($linkcode, $result);
$result=$linkcode;
return $result;
}
elseif($whattoreturn=="3")
{
$alinkstart="";
$strtoreplace=$stringtofind;
$strurl=$url;
$startoflinkcode=strstr($stringtosearch, $alinkstart);
/*if($startoflinkcode!=false)
{
$totallinkcode=strstr($startoflinkcode, $alinkend, true)."";
$linkcodelen=strlen($totallinkcode);
$textonlystr1=strstr($stringtosearch, $alinkstart, true);
$textonlystr2=substr($stringtosearch2,$linkcodelen);
$stringtosearch2=$textonlystr2;
}*/
$result=str_replace($strtoreplace, $linkcode, $stringtosearch2);
return $result;
}
}
function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
function isPhone($data) {
$isphone=false;
if(preg_match("/^[0-9]{3}-[0-9]{3}-[0-9]{4}$/", $data)) {
  // $phone is valid
$isphone=true;
}
return $isphone;
}
/**
 * Creates an 80-bit, binary(10) unique identifier.
 * 
 * Smaller, so indexes fit better in memory/RAM and the hex representation 
 * is still relatively URL friendly. Sequential, so inserts/indexes faster. 
 * (see https://www.percona.com/blog/2014/12/19/store-uuid-optimized-way/)
 *  
 * 4 bit worker id (supports 16 servers, from 0 to 15)
 * 60 bit timestamp
 * 16 bit random clock sequence
 * 
 * Here are some useful MySQL calls: 
 * SELECT DATE_FORMAT(FROM_UNIXTIME(CONV(SUBSTR(HEX(people.id), 2, 15),16,10)/10000000),'%d %b %Y %T:%f') FROM people; 
 **/
class Snowflake
{
    /**
     * Time (in 100ns steps) between the start of the UTC and Unix epochs
     * @var int
     */
    const INTERVAL = 0x01b21dd213814000;
    private $hex;
    public function __construct($hex = null, $worker = "0")
    {
        if (empty($hex)) {
            $hex = $this->makeHex($worker);
        }
        $this->hex = $hex;
    }
    public static function create($worker = "0")
    {
        return new Snowflake(null, $worker);
    }
    public static function createFromExisting($hex)
    {
        return new Snowflake($hex);
    }
    private function makeHex($worker)
    {
        // Convert decimal worker (0 to 15) to single digit hex (0 to F)
        $worker = dechex($worker);
        /** Get time since Gregorian calendar reform in 100ns intervals
         * This is exceedingly difficult because of PHP's (and pack()'s)
         * integer size limits.
         * Note that this will never be more accurate than to the microsecond.
         * You can also change the interval.
         */
        // $timestamp = microtime(1) * 10000000 + static::INTERVAL;
        $timestamp = microtime(1) * 10000000;
        // Convert to a string representation
        $timestamp = sprintf("%F", $timestamp);
        //strip decimal point
        preg_match("/^\d+/", $timestamp, $timestamp);
        // And now to a 64-bit binary representation that begins with the worker id
        $snowflake = base_convert($timestamp[0], 10, 16);
        $snowflake = str_pad($snowflake, 16, dechex($worker), STR_PAD_LEFT);
        // Generate a random clock sequence (2 bytes)
        $snowflake .= bin2hex(openssl_random_pseudo_bytes(2));
        return $snowflake;
    }
    public function getHex(){
        return $this->hex;
    } 
    public function getDateTime($format = 'd M Y h:i:s A')
    {
        $timestamp = base_convert(substr($this->hex, 1, 15), 16, 10);
        $timestamp = sprintf("%d", $timestamp);
        // $timestamp = ($timestamp) / 10000000 - static::INTERVAL;
        $timestamp = ($timestamp) / 10000000;
        return date($format, $timestamp);
    }
    
}
function getKeyArray()
{
	$kstr="";
	$keyarry=array();
for($i=0; $i<48; $i++)
{
	$kstr="$i";
	array_push($keyarry, $kstr);
}
return $keyarry;
}
function initializeBingoBallsArray()
{
$bingoballsarray=array();
for($i=0; $i<75; $i++)
{
array_push($bingoballsarray, $i+1);
}
return $bingoballsarray;
}
function PickRandomIbongNumbers()
{
$ballsarry=initializeBingoBallsArray();
$ballsarry2=$ballsarry;
$ibongcalledballs=array();
$ballsarrysize=sizeof($ballsarry2)-1;
for($callnum=1; $callnum<=48; $callnum++)
{
$pickedindex=IbongPick(0, $ballsarrysize);
//$ballsarry[$pickedindex];
array_push($ibongcalledballs, $ballsarry2[$pickedindex]);
$to_remove = array($ballsarry2[$pickedindex]);
$ballsarry2 = array_diff($ballsarry2, $to_remove);
//for($i=0; $i$arry[$i-1]);
		
		array_push($assocarry, $a);
	}
	return $assocarry;
}
function parseNumToReadableWord($num)
{
if(strlen($num)>1)
{
}
}
function getReadableNumStringFromNum($num)
{
$word="";
switch($num)
{
case "0":
$word="zero";
break;
case "1":
$word="one";
break;
case "2":
$word="two";
break;
case "3":
$word="three";
break;
case "4":
$word="four";
break;
case "5":
$word="five";
break;
case "6":
$word="six";
break;
case "7":
$word="seven";
break;
case "8":
$word="eight";
break;
case "9":
$word="nine";
break;
case "10":
$word="ten";
break;
case "11":
$word="eleven";
break;
case "12":
$word="twelve";
break;
case "13":
$word="thirteen";
break;
case "14":
$word="fourteen";
break;
case "15":
$word="fifteen";
break;
}
return $word;
}
function parseKeyToArray($keytoparse)
{
$keyarray=array();
for($i=0; $igetOffset(new DateTime);
    }
    // sort timezone by offset
    asort($timezone_offsets);
    $timezone_list = array();
    foreach( $timezone_offsets as $timezone => $offset )
    {
        $offset_prefix = $offset < 0 ? '-' : '+';
        $offset_formatted = gmdate( 'H:i', abs($offset) );
        $pretty_offset = "UTC${offset_prefix}${offset_formatted}";
        $timezone_list[$timezone] = "(${pretty_offset}) $timezone";
    }
    return $timezone_list;
}
function timezone_list() {
    static $timezones = null;
    if ($timezones === null) {
	    $timezones = array();
        $offsets = array();
        $now = new DateTime('now', new DateTimeZone('UTC'));
        foreach (DateTimeZone::listIdentifiers() as $timezone) {
            $now->setTimezone(new DateTimeZone($timezone));
            $offsets[] = $offset = $now->getOffset();
            $timezones[$timezone] = '(' . format_GMT_offset($offset) . ') ' . $timezone;
        }
        array_multisort($offsets, $timezones);
    }
    return $timezones;
}
function format_GMT_offset($offset) {
    $hours = intval($offset / 3600);
    $minutes = abs(intval($offset % 3600 / 60));
    return 'GMT' . ($offset ? sprintf('%+03d:%02d', $hours, $minutes) : '+00:00');
}
function format_timezone_name($name) {
    $name = str_replace('/', ', ', $name);
    $name = str_replace('_', ' ', $name);
    $name = str_replace('St ', 'St. ', $name);
    return $name;
}
function cmpdatetime($a, $b) {
    $ta = $a['broadcast_dateunixtime'];
    $tb = $b['broadcast_dateunixtime'];
    //return ($ta>$tb)?-1:1;
	return ($ta - $tb);
}
function getClosestDate($arrytocheck, $timestamptocheckagainst)
{
$entrydbidtoreturn="";
foreach($arrytocheck as $entryarry)
{
	
$entrytimestamp=$entryarry['broadcast_dateunixtime'];
$dbid=$entryarry['id'];
if($entrytimestamp>$timestamptocheckagainst)
{
$entrydbidtoreturn=$dbid;
break;
}
else
{
if(empty($entrydbidtoreturn) || $entrydbidtoreturn=="")
{
$entrydbidtoreturn=$dbid;
}
}
}
return $entrydbidtoreturn;
}
function fetchAssociativeArrayEntry($id, $arrytofetch)
{
	
}
function MMDDYYYYtoDateDiffFormat($datestr)
{
	$pos=strripos($datestr, "-");
	if($pos!==FALSE)
	{
	$monthday=substr($datestr, 0, $pos);
	$year=substr($datestr,$pos+1);
	$newdatestr_datediffformat=$year."-".$monthday;
	}
}
// Define a function to output files in a directory
function outputFilesAsArray($path, $fnametomatch, $type){
	$arrayfiles=array();
	$allowedExts=array($type);
	// Check directory exists or not
    if(file_exists($path) && is_dir($path)){
        // Scan the files in this directory
        $result = scandir($path);
        
        // Filter out the current (.) and parent (..) directories
        $files = array_diff($result, array('.', '..'));
        
        if(count($files) > 0){
            // Loop through retuned array
            foreach($files as $file){
                if(is_file("$path/$file")){
                    // Display filename
					$temp = explode(".", $file);
					$extension = end($temp);
					$filename=current($temp);
					if(in_array($extension, $allowedExts) && in_array($filename, $fnametomatch))
					{
						if(count($arrayfiles) > 0)
						{
						array_push($arrayfiles, $path."/".$file);
						}
						else
						{
						$arrayfiles=array($path."/".$file);
						}
					}
                    //echo $path."/".$file."
";
                }/* else if(is_dir("$path/$file")){
                    // Recursively call the functon if directories found
					// Display foldername
                    //echo $path."/".$file."/
";
                   $images2=outputFilesAsArray("$path/$file", "$type");
				   $images=array_merge($images, $images2);
                }*/
            }
        } else{
            //echo "ERROR: No files found in the directory.";
        }
    } else {
        //echo "ERROR: The directory does not exist.";
    }
	return $images;
}
// Define a function to output files in a directory
function outputFiles($path){
    // Check directory exists or not
    if(file_exists($path) && is_dir($path)){
        // Scan the files in this directory
        $result = scandir($path);
        
        // Filter out the current (.) and parent (..) directories
        $files = array_diff($result, array('.', '..'));
        
        if(count($files) > 0){
            // Loop through retuned array
            foreach($files as $file){
                if(is_file("$path/$file")){
                    // Display filename
                    echo $path."/".$file."
";
                } else if(is_dir("$path/$file")){
                    // Recursively call the functon if directories found
					// Display foldername
                    echo $path."/".$file."/
";
                    outputFiles("$path/$file");
                }
            }
        } else{
            echo "ERROR: No files found in the directory.";
        }
    } else {
        echo "ERROR: The directory does not exist.";
    }
}
/**
	Method to execute a command in the terminal
	Uses :
	
	1. system
	2. passthru
	3. exec
	4. shell_exec
*/
function terminal($command)
{
	//system
	if(function_exists('system'))
	{
		ob_start();
		system($command , $return_var);
		$output = ob_get_contents();
		ob_end_clean();
	}
	//passthru
	else if(function_exists('passthru'))
	{
		ob_start();
		passthru($command , $return_var);
		$output = ob_get_contents();
		ob_end_clean();
	}
	
	//exec
	else if(function_exists('exec'))
	{
		exec($command , $output , $return_var);
		$output = implode('" "', $output);
	}
	
	//shell_exec
	else if(function_exists('shell_exec'))
	{
		$output = shell_exec($command) ;
	}
	
	else
	{
		$output = 'Command execution not possible on this system';
		$return_var = 1;
	}
	
	return array('output' =>'> '.$output , 'status' =>'> '.$return_var);
}
function BooleanArrayIsTrue(array $boolarry)
{
	$result=false;
	for($i=0; $i''.$classarry[0], 'page-item-id'=>''.$classarry[1], 'url'=>''.$ahref_linkurl, 'label'=>''.$ahref_linklabel);
array_push($wpmulit_page_array, $wp_li_elements_arry);
}
return $wpmulit_page_array;
//return $wp_li_elements_arry;
}
function return_wp_list_pagesarray($wplistpages)
{
return explode("\r\n",$wplistpages);
}
function return_wp_li_classnames($wplistpages)
{
$litagpos=stripos($wplistpages, "");
$litagendlen=strlen("\">");
if($litagpos2!=FALSE)
{
$classnamesstr=substr($classnames, 0, $litagpos2);
}
}
$classnames_arry=explode(" ", $classnamesstr);
return $classnames_arry;
}
function return_wp_ahref_url($wplistpages)
{
$ahreftagpos=stripos($wplistpages, "");
$ahreftagendlen=strlen("\">");
if($ahreftagpos2!=FALSE)
{
$ahref_link_url=substr($ahrefurl, 0, $ahreftagpos2);
}
}
//$classnames_arry=explode(" ", $classnamesstr);
//return $classnames_arry;
return $ahref_link_url;
}
function return_wp_ahref_link_label($wplistpages)
{
$ahreftagpos=stripos($wplistpages, "");
$ahreftagendlen=strlen("\">");
if($ahreftagpos2!=FALSE)
{
	
$ahref_link_label=substr($ahrefurl, $ahreftagpos2+$ahreftagendlen);
$ahrefendtagpos=stripos($ahref_link_label, "");
$ahrefendtaglen="";
if($ahrefendtagpos!=FALSE)
{
$ahref_link_labelstr=substr($ahref_link_label, $ahrefendtagpos);
}
}
}
//$classnames_arry=explode(" ", $classnamesstr);
//return $classnames_arry;
return $ahref_link_labelstr;
}
//echo "Hex: ". $uid->getHex()."\n";         // Hex: 003349028aca2aca3642
//echo "DateTime: " . $uid->getDateTime();   // DateTime: 29 Sep 2015 08:05:07 PM
?>